﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.FxCop.Sdk;
using System.Reflection;
using FxCopContrib.TestHarness;

namespace FxCopContrib.Tests
{
    [TestClass]
    public class DoNotIgnoreResultOfImmutableMethodsTests
    {
        public TestContext TestContext { get; set; }

        [TestMethod]
        [DeploymentItem("securityrules.dll")]
        public void RuleMustTriggerWhenMethodCallToImmutableTypeIsIgnored()
        {
            FxCopTest
                .ForRule<DoNotIgnoreResultOfImmutableMethods>()
                .OnMethod(
                    @"
                        public void TestMethod()
                        {
                            DateTime.UtcNow.AddHours(1);
                        }
                    "
                )
                .WithReferencesTo(typeof(DateTime))
                .WillFind(ProblemPrototype.Default)
                .Conclusive()
                .Verify();
        }

        [TestMethod]
        public void RuleWillNotTriggerOnIgnoredResultsFromMutableTypes()
        {
            FxCopTest
                .ForRule<DoNotIgnoreResultOfImmutableMethods>()
                .OnMethod(
                    @"
                        public void TestMethod()
                        {
                            File.Exists("""");
                        }
                    ", 
                    "System.IO"
                )
                .WithReferencesTo(typeof(DateTime))
                .WillNotFind(ProblemPrototype.Default)
                .Conclusive()
                .Verify();
        }

 
        [TestMethod]
        public void RuleMustNotTriggerWhenMethodCallToImmutableTypeIsAssignedToALocal()
        {
            FxCopTest
                .ForRule<DoNotIgnoreResultOfImmutableMethods>()
                .OnMethod(
                    @"
                        public void TestMethod()
                        {
                            DateTime value = DateTime.UtcNow.AddHours(1);
                        }
                    "
                )
                .WithReferencesTo(typeof(DateTime))
                .WillNotFind(ProblemPrototype.Default)
                .Conclusive()
                .Verify();
        }

        [TestMethod]
        public void RuleMustNotTriggerWhenMethodCallToImmutableTypeIsPassedToAnotherMethod()
        {
            FxCopTest
                .ForRule<DoNotIgnoreResultOfImmutableMethods>()
                .OnMethod(
                    @"
                        public void TestMethod()
                        {
                            OtherMethod(DateTime.UtcNow.AddHours(1));
                        }

                        public void OtherMethod(DateTime value){}
                    "
                )
                .WithReferencesTo(typeof(DateTime))
                .WillNotFind(ProblemPrototype.Default)
                .Conclusive()
                .Verify();
        }
    }


}
